package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.mumzone.basic.adapter.rv.CommonAdapter;
import cn.mumzone.basic.adapter.rv.ViewHolder;
import cn.mumzone.basic.util.AppDateUtil;
import cn.mumzone.basic.util.BigDecimalUtil;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.InventoryDetailEntity;
import cn.mumzone.pos.model.InventoryGoodsEntity;
import cn.mumzone.pos.ui.widgets.keyboard.PosKeyBoardView;
import cn.mumzone.pos.ui.widgets.shopping.AnimShopButton;
import cn.mumzone.pos.util.GoodsUtil;

/**
 * Created by qiuzhenhuan on 2019/10/11.
 */

public class ADA_InventoryGoods extends CommonAdapter<InventoryGoodsEntity> {

    private Context context;

    private String inventoryStatus;


    public ADA_InventoryGoods(Context context) {
        super(context);
        this.context = context;
        inventoryStatus = InventoryDetailEntity.INVENTORY_STATUS_NO_START;
    }


    @Override
    protected int itemLayoutId() {
        return R.layout.item_inventory_goods;
    }

    @Override
    protected void convert(ViewHolder holder, final InventoryGoodsEntity goodsListBean, final int position) {

        holder.setText(R.id.tv_good_name, StringUtils.get(goodsListBean.getGoodsName()));
        holder.setText(R.id.tv_good_spec,
                String.format("规格：%s", StringUtils.get(goodsListBean.getGoodsSpec(), "无")));


        final AnimShopButton vInventoryCount = holder.getView(R.id.v_goods_count);
        LinearLayout llBatchGoods = holder.getView(R.id.ll_batch_goods);

        if (!ListUtil.isListEmpty(goodsListBean.getGoodsBatchNumList()) && !StringUtils.isEmpty(goodsListBean.getGoodsBatchNumList().get(0).getGoodsBatchNumber())) {//批次商品
            holder.setVisible(R.id.rl_batch, false);
            llBatchGoods.setVisibility(View.VISIBLE);
            llBatchGoods.removeAllViews();
            for (final InventoryGoodsEntity.GoodsBatchNumListBean entity : goodsListBean.getGoodsBatchNumList()) {
                View batchView = LayoutInflater.from(context).inflate(R.layout.item_goods_batch_inventory, llBatchGoods, false);
                TextView tvProductionDate = batchView.findViewById(R.id.tv_production_date);
                TextView tvBatch = batchView.findViewById(R.id.tv_batch);
                TextView tvStockCount = batchView.findViewById(R.id.tv_stock_count);
                TextView tvInventoryCountTitle = batchView.findViewById(R.id.tv_goods_count_title);
                TextView tvInventoryCount = batchView.findViewById(R.id.tv_goods_count);
                if(!StringUtils.isEmpty(entity.getUnitName())){
                    tvInventoryCountTitle.setText(String.format("盘点数量(%s)：", StringUtils.get(entity.getUnitName())));
                }else {
                    tvInventoryCountTitle.setText("盘点数量：");
                }
                final AnimShopButton vInventoryBatchCount = batchView.findViewById(R.id.v_goods_count);

                if (InventoryDetailEntity.INVENTORY_STATUS_PROGRESSING.equals(inventoryStatus)) {//进行中
                    tvInventoryCountTitle.setVisibility(View.VISIBLE);
                    tvInventoryCount.setVisibility(View.GONE);
                    vInventoryBatchCount.setVisibility(View.VISIBLE);
                    vInventoryBatchCount.setCount(Integer.parseInt(BigDecimalUtil.getNumber(entity.getGoodsNum())));
                    vInventoryBatchCount.setOnAddDelListener(new GoodsShopButtonAdapter(mContext) {
                        @Override
                        public void onAddSuccess(int count) {
                            super.onAddSuccess(count);
                            entity.setGoodsNum(String.valueOf(count));
                            if (onGoodsCountChangeListener != null) {
                                onGoodsCountChangeListener.onGoodsCountChanged(goodsListBean.getGoodsId(), entity.getId(), count);
                            }
                        }

                        @Override
                        public void onDelSuccess(int count) {
                            super.onDelSuccess(count);
                            entity.setGoodsNum(String.valueOf(count));
                            if (onGoodsCountChangeListener != null) {
                                onGoodsCountChangeListener.onGoodsCountChanged(goodsListBean.getGoodsId(), entity.getId(), count);
                            }

                        }

                        @Override
                        public void onInputAreaClicked() {
                            super.onInputAreaClicked();

                            GoodsUtil.showModifyCountDialog(mContext, new PosKeyBoardView.OnConfirmClickListener() {
                                @Override
                                public void onConfirmClick(String text) {
                                    entity.setGoodsNum(String.valueOf(text));
                                    vInventoryBatchCount.setCount(Integer.valueOf(text));
                                    if (onGoodsCountChangeListener != null) {
                                        onGoodsCountChangeListener.onGoodsCountChanged(goodsListBean.getGoodsId(), entity.getId(), Integer.valueOf(text));
                                    }
                                }
                            });
                        }
                    });
                } else if (InventoryDetailEntity.INVENTORY_STATUS_DONE.equals(inventoryStatus)) {
                    tvInventoryCountTitle.setVisibility(View.VISIBLE);
                    vInventoryBatchCount.setVisibility(View.GONE);
                    tvInventoryCount.setVisibility(View.VISIBLE);
                    tvInventoryCount.setText(BigDecimalUtil.getNumber(entity.getGoodsNum()));

                } else {
                    tvInventoryCountTitle.setVisibility(View.GONE);
                    tvInventoryCount.setVisibility(View.GONE);
                    vInventoryBatchCount.setVisibility(View.GONE);
                }

                if (!StringUtils.isEmpty(entity.getDateInProduced())) {
                    tvProductionDate.setText(
                            AppDateUtil.getDateToString(Long.valueOf(StringUtils.get(entity.getDateInProduced())), AppDateUtil.YYYY_MM_DD1));
                } else {
                    tvProductionDate.setText("");
                }
                tvBatch.setText(StringUtils.get(entity.getGoodsBatchNumber()));
                tvStockCount.setText(String.format("%s %s",
                        BigDecimalUtil.getNumber(entity.getSkuNum()), StringUtils.get(entity.getUnitName())));
                llBatchGoods.addView(batchView);
            }
        } else {
            //非批次商品
            llBatchGoods.setVisibility(View.GONE);
            holder.setVisible(R.id.rl_batch, true);
            final InventoryGoodsEntity.GoodsBatchNumListBean batchEntity = goodsListBean.getGoodsBatchNumList().get(0);
            holder.setText(R.id.tv_stock_count, String.format("%s %s",
                    BigDecimalUtil.getNumber(batchEntity.getSkuNum()), StringUtils.get(batchEntity.getUnitName())));
            if(!StringUtils.isEmpty(batchEntity.getUnitName())){
                holder.setText(R.id.tv_goods_count_title, String.format("盘点数量(%s)：", StringUtils.get(batchEntity.getUnitName())));
            }else {
                holder.setText(R.id.tv_goods_count_title, "盘点数量：");
            }

            if (InventoryDetailEntity.INVENTORY_STATUS_PROGRESSING.equals(inventoryStatus)) {//进行中
                holder.setVisible(R.id.tv_goods_count_title, true);
                holder.setVisible(R.id.tv_goods_count, false);
                vInventoryCount.setVisibility(View.VISIBLE);
                final int inventoryCount = Integer.parseInt(BigDecimalUtil.getNumber(batchEntity.getGoodsNum()));
                vInventoryCount.setCount(inventoryCount);
                vInventoryCount.setOnAddDelListener(new GoodsShopButtonAdapter(mContext) {
                    @Override
                    public void onAddSuccess(int count) {
                        super.onAddSuccess(count);
                        batchEntity.setGoodsNum(String.valueOf(count));
                        if (onGoodsCountChangeListener != null) {
                            onGoodsCountChangeListener.onGoodsCountChanged(goodsListBean.getGoodsId(), batchEntity.getId(), count);
                        }
                    }

                    @Override
                    public void onDelSuccess(int count) {
                        super.onDelSuccess(count);
                        batchEntity.setGoodsNum(String.valueOf(count));
                        if (onGoodsCountChangeListener != null) {
                            onGoodsCountChangeListener.onGoodsCountChanged(goodsListBean.getGoodsId(), batchEntity.getId(), count);
                        }
                    }

                    @Override
                    public void onInputAreaClicked() {
                        super.onInputAreaClicked();
                        GoodsUtil.showModifyCountDialog(mContext, new PosKeyBoardView.OnConfirmClickListener() {
                            @Override
                            public void onConfirmClick(String text) {
                                batchEntity.setGoodsNum(String.valueOf(text));
                                vInventoryCount.setCount(Integer.valueOf(text));
                                if (onGoodsCountChangeListener != null) {
                                    onGoodsCountChangeListener.onGoodsCountChanged(goodsListBean.getGoodsId(), batchEntity.getId(), Integer.parseInt(text));
                                }
                            }
                        });
                    }
                });
            } else if (InventoryDetailEntity.INVENTORY_STATUS_DONE.equals(inventoryStatus)) {//已完成
                holder.setVisible(R.id.tv_goods_count_title, true);
                holder.setVisible(R.id.tv_goods_count, true);
                vInventoryCount.setVisibility(View.GONE);
                int inventoryCount = Integer.parseInt(BigDecimalUtil.getNumber(batchEntity.getGoodsNum()));
                holder.setText(R.id.tv_goods_count, String.valueOf(inventoryCount));

            } else {
                holder.setVisible(R.id.tv_goods_count_title, false);
                holder.setVisible(R.id.tv_goods_count, false);
                vInventoryCount.setVisibility(View.GONE);
            }
        }
    }

    public void setInventoryStatus(String inventoryStatus) {
        this.inventoryStatus = inventoryStatus;
    }


    private OnGoodsCountChangeListener onGoodsCountChangeListener;

    public void setOnGoodsCountChangeListener(OnGoodsCountChangeListener onGoodsCountChangeListener) {
        this.onGoodsCountChangeListener = onGoodsCountChangeListener;
    }

    public interface OnGoodsCountChangeListener {
        void onGoodsCountChanged(String goodsId, String batchId, int count);
    }
}
